package com.superzqb.asus.superzqbFragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.superzqb.asus.Api.ApiParam;
import com.superzqb.asus.Api.ShopApi;
import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.superzqbShopMain.Adapter.ShopAdapter;
import com.superzqb.asus.superzqbFragment.superzqbShopMain.Bean.GoodsTypeBean;
import com.superzqb.asus.superzqbFragment.superzqbShopMain.Shop.GoodsDetail;
import com.superzqb.asus.superzqbUtil.MyItemClicklistener;
import com.superzqb.asus.superzqbUtil.Url;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by asus on 2017/1/17.
 */

public class ShopContentFragment extends Fragment implements MyItemClicklistener {
    List<GoodsTypeBean> list;
    ShopAdapter adapter;
    private int pagenum=1;
    private int type;

    public ShopContentFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.shop_phone, container, false);
        list=new ArrayList<>();
        type=getArguments().getInt("type");
        getList();
        adapter=new ShopAdapter(getActivity(),list);
        RecyclerView myHolder=(RecyclerView)view.findViewById(R.id.recycler);
        GridLayoutManager l=new GridLayoutManager(getActivity(),3);
        l.setOrientation(GridLayoutManager.VERTICAL);
        myHolder.setLayoutManager(l);
        myHolder.setAdapter(adapter);
        myHolder.setItemAnimator(new DefaultItemAnimator());
        adapter.setOnItemClickListener(this);
        return view;

    }

    public void getList() {
        Retrofit retrofit= new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(new Url().getBase_url())
                .build();
        ShopApi api=retrofit.create(ShopApi.class);
        Call<List<GoodsTypeBean>> call=api.getTypeList(pagenum, type);
        call.enqueue(new Callback<List<GoodsTypeBean>>() {
            @Override
            public void onResponse(Call<List<GoodsTypeBean>> call, Response<List<GoodsTypeBean>> response) {
                if(response.isSuccessful()){
                    List<GoodsTypeBean> tmp=response.body();
                   // for(int a=0;a<10;a++)
                    list.addAll(tmp);
                    //Toast.makeText(getActivity(),"正在查找，请稍后",Toast.LENGTH_SHORT).show();
                    adapter.setList(list);
                }

            }

            @Override
            public void onFailure(Call<List<GoodsTypeBean>> call, Throwable throwable) {
                //Toast.makeText(getActivity(),"正在查找，请稍后",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onItemClick(View view, int postion) {
       // Toast.makeText(getActivity(),"点击"+list.get(postion).getId(), Toast.LENGTH_SHORT).show();
        Intent intent=new Intent(getActivity(),GoodsDetail.class);
        intent.putExtra("id",list.get(postion).getId()+"");
        intent.putExtra("name",list.get(postion).getName());
        intent.putExtra("images", (Serializable) list.get(postion).getImages());
        getActivity().startActivity(intent);
    }
}
